package com.hjq.widget.view;

import android.content.Context;
import android.graphics.drawable.ClipDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.RelativeLayout;

public class PartialView extends RelativeLayout {
    private ImageView mFilledView;
    private ImageView mEmptyView;

    public PartialView(Context context) {
        super(context);
        this.init();
    }

    public PartialView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.init();
    }

    public PartialView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.init();
    }

    private void init() {
        this.mFilledView = new ImageView(this.getContext());
        this.mFilledView.setScaleType(ScaleType.CENTER_CROP);
        this.mEmptyView = new ImageView(this.getContext());
        this.mEmptyView.setScaleType(ScaleType.CENTER_CROP);
        this.addView(this.mFilledView);
        this.addView(this.mEmptyView);
        this.setEmpty();
    }

    public void setFilledDrawable(Drawable drawable) {
        if(drawable.getConstantState() != null) {
            ClipDrawable clipDrawable = new ClipDrawable(drawable.getConstantState().newDrawable(), 8388611, 1);
            this.mFilledView.setImageDrawable(clipDrawable);
        }
    }

    public void setEmptyDrawable(Drawable drawable) {
        if(drawable.getConstantState() != null) {
            ClipDrawable clipDrawable = new ClipDrawable(drawable.getConstantState().newDrawable(), 8388613, 1);
            this.mEmptyView.setImageDrawable(clipDrawable);
        }
    }

    public void setFilled() {
        this.mFilledView.setImageLevel(10000);
        this.mEmptyView.setImageLevel(0);
    }

    public void setPartialFilled(float rating) {
        float percentage = rating % 1.0F;
        int level = (int)(10000.0F * percentage);
        level = level == 0?10000:level;
        this.mFilledView.setImageLevel(level);
        this.mEmptyView.setImageLevel(10000 - level);
    }

    public void setEmpty() {
        this.mFilledView.setImageLevel(0);
        this.mEmptyView.setImageLevel(10000);
    }
}
